﻿#include "SetScriptDialog.h"

#include "qlist.h"
#include "qmessagebox.h"
#include "SetScriptWaitDialog.h"
#include "SetScriptLoopDialog.h"
#include "Dialog_SetThermostat.h"
#include "SetPositionModeDialog.h"
#include "SetRateModeDialog.h"
#include "TcpCodec_Script.h"

//#include "ScriptInstructionFactory.h"

SetScriptDialog::SetScriptDialog(QWidget* parent) :QDialog(parent)
{
  ui.setupUi(this);
  initSignalsAndSlots();
  ui.pushButton_script_edit->setDisabled(true);
}

SetScriptDialog::~SetScriptDialog() {}

int SetScriptDialog::exec()
{
  connect(tcpCodec_Script,&TcpCodec_Script::signal_script,[this](){});

//  ui.treeWidget_script_list->showScriptList();
  return QDialog::exec();
}

void SetScriptDialog::accept()
{
//  if (ui.treeWidget_script_list->updateScriptList())
    {
      QDialog::accept();
    }
  return;
}

void SetScriptDialog::slot_openSetPositionModeDialog()
{
  if (QDialog::Accepted == setPositionModeDialog->exec())
    {
//      ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("设置位置模式，位置：%1°，速率：%2°/s，加速度：%3°/s²")
//                                                        .arg(setPositionModeDialog->getPosition(),0,'f',4)
//                                                        .arg(setPositionModeDialog->getRate(),0,'f',4)
//                                                        .arg(setPositionModeDialog->getAcceleration(),0,'f',4));
    }
}
void SetScriptDialog::slot_openSetRateModeDialog()
{
  if (QDialog::Accepted == setRateModeDialog->exec())
    {
//      ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("设置速率模式，速率：%1°/s，加速度：%2°/s²")
//                                                        .arg(setRateModeDialog->getRate(),0,'f',4)
//                                                        .arg(setRateModeDialog->getAcceleration(),0,'f',4));
    }
}
void SetScriptDialog::slot_openDialog_SetThermostat()
{
  if (QDialog::Accepted == dialog_SetThermostat->exec())
    {
//      ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("设置温箱变温速率，速率：%1℃/m").arg(setThermostatDialog->getRate(),0,'f',4));
//      ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("设置温箱目标温度，温度：%1℃").arg(setThermostatDialog->getTemperature(),0,'f',4));
    }
}
void SetScriptDialog::slot_openSetWaitTimeDialog()
{
  if (QDialog::Accepted == setScriptWaitDialog->exec())
    {
//      ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("等待，时间：%1s").arg(setScriptWaitDialog->getWaitTime()/1000));
    }
}
void SetScriptDialog::slot_openSetLoopDialog()
{
  if (QDialog::Accepted == setScriptLoopDialog->exec())
    {
//      ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("循环，次数：%1").arg(setScriptLoopDialog->getLoopCount()));
    }
}
void SetScriptDialog::slot_edit() {}


void SetScriptDialog::initSignalsAndSlots()
{
//  connect(ui.pushButton_turnTable_powerOn, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台上电")); });
//  connect(ui.pushButton_turnTable_powerOff, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台下电")); });
//  connect(ui.pushButton_turnTable_closeCircuit, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台闭合")); });
//  connect(ui.pushButton_turnTable_openCircuit, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台闲置")); });
//  connect(ui.pushButton_turnTable_unloadedMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台空载")); });
//  connect(ui.pushButton_turnTable_loadedMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台负载")); });
  connect(ui.pushButton_turnTable_setPositionMode, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetPositionModeDialog);
  connect(ui.pushButton_turnTable_setRateMode, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetRateModeDialog);
//  connect(ui.pushButton_turnTable_runPositionMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台运行位置模式")); });
//  connect(ui.pushButton_turnTable_runRateMode, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台运行速率模式")); });
  connect(ui.pushButton_thermostat_setTemperatureAndRate, &QPushButton::clicked, this, &SetScriptDialog::slot_openDialog_SetThermostat);
//  connect(ui.pushButton_turnTable_stop, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台停止")); });
//  connect(ui.pushButton_turnTable_emergencyStop, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->addOperationNameToList(QStringLiteral("转台急停")); });
  connect(ui.pushButton_script_confirm, &QPushButton::clicked, this, &SetScriptDialog::accept);
  connect(ui.pushButton_script_cancel, &QPushButton::clicked, this, &SetScriptDialog::reject);
  connect(ui.pushButton_script_wait, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetWaitTimeDialog);
  connect(ui.pushButton_script_creatLoop, &QPushButton::clicked, this, &SetScriptDialog::slot_openSetLoopDialog);
  connect(ui.pushButton_script_edit, &QPushButton::clicked, this, &SetScriptDialog::slot_edit);
  connect(ui.pushButton_script_clone, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::Clone); });
  connect(ui.pushButton_script_moveUp, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::MoveUp); });
  connect(ui.pushButton_script_moveDown, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::MoveDown); });
  connect(ui.pushButton_script_delete, &QPushButton::clicked, [this]() {ui.treeWidget_script_list->itemOperation(TreeWidget_EditScriptList::Remove); });
}

SetScriptWaitDialog* SetScriptDialog::getSetScriptWaitDialog()
{
  return setScriptWaitDialog;
}
SetScriptDialog *SetScriptDialog::setSetScriptWaitDialog(SetScriptWaitDialog* setScriptWaitDialog)
{
  this->setScriptWaitDialog = setScriptWaitDialog;
  return this;
}
SetScriptLoopDialog* SetScriptDialog::getSetScriptLoopDialog()
{
  return setScriptLoopDialog;
}
SetScriptDialog* SetScriptDialog::setSetScriptLoopDialog(SetScriptLoopDialog* setScriptLoopDialog)
{
  this->setScriptLoopDialog = setScriptLoopDialog;
  return this;
}
Dialog_SetThermostat* SetScriptDialog::getDialog_SetThermostat()
{
  return dialog_SetThermostat;
}
SetScriptDialog* SetScriptDialog::setDialog_SetThermostat(Dialog_SetThermostat* dialog_SetThermostat)
{
  this->dialog_SetThermostat = dialog_SetThermostat;
  return this;
}
SetPositionModeDialog* SetScriptDialog::getSetPositionModeDialog()
{
  return setPositionModeDialog;
}
SetScriptDialog* SetScriptDialog::setSetPositionModeDialog(SetPositionModeDialog* setPositionModeDialog)
{
  this->setPositionModeDialog = setPositionModeDialog;
  return this;
}
SetRateModeDialog* SetScriptDialog::getSetRateModeDialog()
{
  return setRateModeDialog;
}
SetScriptDialog* SetScriptDialog::setSetRateModeDialog(SetRateModeDialog* setRateModeDialog)
{
  this->setRateModeDialog = setRateModeDialog;
  return this;
}

TcpCodec_Script* SetScriptDialog::getTcpCodec_Script()
{
  return tcpCodec_Script;
}

SetScriptDialog* SetScriptDialog::setTcpCodec_Script(TcpCodec_Script* tcpCodec_Script)
{
  this->tcpCodec_Script=tcpCodec_Script;
  printInjection(this,tcpCodec_Script);
  return this;
}

void SetScriptDialog::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setSetScriptWaitDialog(getObjFromMap<SetScriptWaitDialog>(objMap));
  setSetScriptLoopDialog(getObjFromMap<SetScriptLoopDialog>(objMap));
  setDialog_SetThermostat(getObjFromMap<Dialog_SetThermostat>(objMap));
  setSetPositionModeDialog(getObjFromMap<SetPositionModeDialog>(objMap));
  setSetRateModeDialog(getObjFromMap<SetRateModeDialog>(objMap));
  setTcpCodec_Script(getObjFromMap<TcpCodec_Script>(objMap));
}
